package com.open.party.cloud.view.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.open.party.cloud.R;
import com.open.party.cloud.model.TownshipBean;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *  创建:  梁玉涛 2019/3/29 on 11:24
 *  项目:
 *  描述:
 *  更新:
 * <pre>
 */
public class ImageShowAdapter extends RecyclerView.Adapter<ImageShowAdapter.ItemViewHolderCommon> {

    private static int TYPE_ADD = 0;//添加图片
    private static int TYPE_COMMON = 1;//普通图片展示
    private Context context;
    private LayoutInflater mLayoutInflater;
    //data
    private List<TownshipBean.ShowFileListBean> imgSelectList;//图片url集合

    public ImageShowAdapter(Context context, ArrayList<TownshipBean.ShowFileListBean> path) {
        this.context = context;
        this.mLayoutInflater = LayoutInflater.from(context);

        this.imgSelectList = path;
    }

    @Override
    public ImageShowAdapter.ItemViewHolderCommon onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolderCommon(mLayoutInflater.inflate(R.layout.image_selected_view_common, parent, false));
    }

    @Override
    public void onBindViewHolder(final ItemViewHolderCommon holder, int position) {
        TownshipBean.ShowFileListBean fileBean = imgSelectList.get(position);
        View itemView = holder.itemView;

        if ("sp".equals(fileBean.getFileType())) {
            Glide.with(context).load("").placeholder(R.drawable.default_dang_play).into(holder.ivCommon);
        } else {
            Glide.with(context).load(fileBean.getImgUrl()).placeholder(R.drawable.icon_default_img).into(holder.ivCommon);
        }

        holder.delImgBtn.setVisibility(View.GONE);

        if (mOnItemClickListener != null) {

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, position, fileBean);
                }
            });

        }
    }

//    @Override
//    public int getItemViewType(int position) {
//        return position == imgSelectList.size() ? TYPE_ADD : TYPE_COMMON;
//    }

    @Override
    public int getItemCount() {
//        if (imgSelectList.size() < mMaxAlbum) {
//            return imgSelectList.size() + 1;//加一代表最后一个添加图片按钮
//        } else {
        return imgSelectList.size();//加一代表最后一个添加图片按钮,已是最大数据则隐藏添加按钮
//        }
    }

    public void setData(ArrayList<TownshipBean.ShowFileListBean> path) {
        if (path == null) {
            path = new ArrayList<>();
        }

        if (this.imgSelectList != null) {
            this.imgSelectList.clear();
        }

        this.imgSelectList.addAll(path);
        notifyDataSetChanged();
    }

    public static class ItemViewHolderAdd extends RecyclerView.ViewHolder {
//        private TextView tvNum;

        public ItemViewHolderAdd(View itemView) {
            super(itemView);
//            tvNum = itemView.findViewById(R.id.tv_album_selected_num);
        }
    }

    public static class ItemViewHolderCommon extends RecyclerView.ViewHolder {
        ImageView ivCommon;
        ImageView delImgBtn;

        public ItemViewHolderCommon(View itemView) {
            super(itemView);
            ivCommon = itemView.findViewById(R.id.iv_album_selected);
            delImgBtn = itemView.findViewById(R.id.delImgBtn);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position, TownshipBean.ShowFileListBean data);
    }


    private OnItemDelClickListener itemDelClick;

    public void setItemDelClick(OnItemDelClickListener itemDelClick) {
        this.itemDelClick = itemDelClick;
    }

    public interface OnItemDelClickListener {
        void onItemDelClick(int position);
    }
}
